package com.whc.config;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whc.entity.User;
import com.whc.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;

import java.util.ArrayList;
import java.util.Collection;


/**
 * @author 韦红材
 * @ClassName DBUserDetailsManager
 * @data 2024/12/20 21:25
 * @Version V1.0
 **/

public class DBUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> lw=new LambdaQueryWrapper<User>();
        lw.eq(User::getUsername,username);
        User user = userMapper.selectOne(lw);
        if(user==null){
            throw new UsernameNotFoundException(username);
        }
        System.out.println("user:"+user);
        //TODO 这里直接先给user权限
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(()->"user");
        return new org.springframework.security.core.userdetails.User(
                user.getUsername(),
                user.getPassword(),
                true,
                true, //用户账号是否过期
                true, //用户凭证是否过期
                true, //用户是否未被锁定
                authorities); //权限列表

    }
    @Override
    public void createUser(UserDetails user) {

    }
    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return null;
    }

    @Override
    public void updateUser(UserDetails user) {

    }

    @Override
    public void deleteUser(String username) {

    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    @Override
    public boolean userExists(String username) {
        return false;
    }


}
